$(function () {


    $("#jqGrid").jqGrid({

        url: baseURL+"ymbwithdraw/list",
        datatype: "json",
        colModel: [
            { label: '手机号', name: 'phone', sortable:false, width: 56 , formatter: function (value, options, row) {
                    if (row.orderState == 0){
                        if(hasPermission("ymbwithdraw:withdraw")) {
                            return "<a href='javascript:vm.lock(" + row.id + ",\""+row.bankNo+"\",\""+row.bankAccount+"\",\""+row.bankName+"\",\""+row.bankBranch+"\");'>" + value +  "</a>";
                        }else{
                            return "" + value + "";
                        }
                    }else{
                        return "" + value + "";
                    }
                }},
            { label: '金额', name: 'money', sortable:false, width: 56 },
            { label: '订单状态', name: 'orderState', width: 38, formatter: function(value, options, row){
                if(value==0)
                    return '<span class="label label-info">待处理</span>' ;
                if(value==1)
                    return '<span class="label label-success">成功</span>' ;
                return '<span class="label label-danger">撤回</span>' ;
            }},
            { label: '结果描述', name: 'description', sortable:false, width: 136 },
            { label: '创建时间', name: 'createTime', sortable: false, width: 68, formatter: function (value, options, row) {
                    return fmtDate(value);
                }
            },
            {
                label: '操作', name: 'operation', sortable: false, width: 50, formatter: function (value, options, row) {
                    if (row.orderState == 0){
                        if(hasPermission("ymbwithdraw:withdraw")) {
                            return "<a href='javascript:vm.lock(" + row.id + ",\""+row.bankNo+"\",\""+row.bankAccount+"\",\""+row.bankName+"\",\""+row.bankBranch+"\");'><span class='label label-success'>审核</span></a>&nbsp;&nbsp;";
                        }else return "";
                    }else return "";
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 20,
		rowList : [10,20,30,50,100,200],
        rownumbers: false,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.pageNum",
            total: "page.pages",
            records: "page.total",

        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order",

        },

        gridComplete:function(){
            //隐藏grid底部滚动条

            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });

        }
    });
    jQuery("#jqGrid").jqGrid('navGrid','#jqGrid',{edit:false,add:false,del:false});

    //过滤时间
    var rangetime = $('#filterTime').daterangepicker({
            autoApply:false,
            singleDatePicker:false,
            showDropdowns:true,         // 是否显示年月选择条件
            timePicker: true, 			// 是否显示小时和分钟选择条件
            timePickerIncrement: 10, 	// 时间的增量，单位为分钟
            timePicker24Hour : true,
            opens : 'left', //日期选择框的弹出位置
            startDate: moment().startOf('year'), //startDate和endDate 的值如果跟 ranges 的两个相同则自动选择ranges中的行. 这里选中了清空行
            endDate: moment().endOf('year'),
            ranges: {
                '今日': [moment().startOf('day'), moment().endOf('day')],
                '昨日': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')],
                '最近7日': [moment().subtract(6, 'days'), moment()],
                '最近30日': [moment().subtract(29, 'days'), moment()],
                '本月': [moment().startOf('month'), moment().endOf('month')],
                '上个月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
                '本年': [moment().startOf('year'), moment().endOf('year')],
                '去年': [moment().subtract(1, 'year').startOf('year'), moment().subtract(1, 'year').endOf('year')]
            },
            locale : {
                //format: 'YYYY-MM-DD HH:mm:ss',
                format: 'YYYY-MM-DD HH:mm',
                separator : ' - ',
                customRangeLabel : '自定义',
                applyLabel : '确定',
                cancelLabel : '取消',
                fromLabel : '起始时间',
                toLabel : '结束时间',
                daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                firstDay : 1,
                startDate: moment().startOf('day'),
                endDate: moment().endOf('day')
            }
        },
        function(start, end, label) // 格式化日期显示框
        {
            //选择时间 回调，暂时不做操作。。。
        });

    $('#filterTime').val('');
    setTimeout("$('#filterTime').val('')", 100);

    function fmtDate(inputTime) {
        var date = new Date(inputTime);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    };

});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        dto: {}
    },
    methods: {
        query: function () {

            vm.reload();


        },
        trash: function () {
            $('#filterTime').val('');
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.dto = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改手机号";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.dto.id == null ? baseURL + "ymbwithdraw/save" : baseURL + "ymbwithdraw/update";
            console.log(JSON.stringify(vm.dto));
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.dto),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();

                        });

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        delCol: function (id) {
            if(id == null){
                return ;
            }
            var ids = [id];
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "PUT",
                    url: baseURL + "ymbwithdraw/delete",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        lock: function (id,bankNo,bankAccount,bankName,bankBranch) {
            if(id == null){
                return ;
            }
            $("#bankNo").val(bankNo);
            $("#bankAccount").val(bankAccount);
            $("#bankName").val(bankName);
            $("#bankBranch").val(bankBranch);
            vm.title = "审核";
            vm.dto['userId'] = id;
            layer.open({
                type: 1,
                skin: 'layui-layer-rim',
                title: "审核",
                area: ['588px', '550px'],
                shadeClose: true,
                anim: 1, //动画类型
                shade: 0.01 ,//遮罩透明度
                content: jQuery("#chargeLayer"),
                btn: ['确定','取消'],
                btn1: function (index)
                {
                    if(null==vm.dto.type){
                        alert("请选择审核操作");
                        return false;
                    }
                    if(null==vm.dto.description||vm.dto.description.replace(/(^\s*)|(\s*$)/g, "")==""){
                        alert("请输入原因备注");
                        return false;
                    }

                    var data = {id:id,orderState: vm.dto.type,description:vm.dto.description};
                    confirm('确定要操作吗？', function(){
                    $.ajax({
                        type: "PUT",
					    async:false,
                        url: baseURL + "ymbwithdraw/withdraw",
                        contentType: "application/json",
                        data: JSON.stringify(data),
                        success: function(r){
                            if(r.code == 0){
                                layer.close(index);
                                alert('操作成功', function(index){
                                    vm.reload();
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                    });
                }
            })

        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "PUT",
                    url: baseURL + "ymbwithdraw/delete",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });

                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(baseURL + "ymbwithdraw/info/"+id, function(r){
                vm.dto = r.dto;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var _postData = $("#jqGrid").jqGrid('getGridParam','postData');
            delete _postData['stime'];
            delete _postData['etime'];
            _postData['p'] = $("#phone").val();
            if((vtime = $.trim($('#filterTime').val())) != '')
            {
                if(vtime.indexOf(" - ") > -1)
                {
                    _postData['stime'] = getTime($.trim(vtime.split(" - ")[0]));
                    _postData['etime'] = getTime($.trim(vtime.split(" - ")[1]));
                }
                else
                {
                    _postData['stime'] = getTime(vtime);
                }
            }

            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        },
    }



});


